function toPrice(num) {
  if (!/\./.test(num)) return num.toLocaleString();
  return num.toString().replace(/(\d)(?=(\d{3})+\.)/g, function (n) {
    return n + ",";
  });
}

const num = 19940516;
console.log(toPrice(num));
